package com.sojpt.boot.controller.business;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.sojpt.boot.controller.business.validator.LkyTeacherAddValidator;
import com.sojpt.boot.controller.business.validator.LkyTeacherUpdateValidator;
import com.sojpt.boot.ui.iview.IViewController;
import com.sojpt.kit.SoJLogKit;
import com.sojpt.model.DicPopedom;
import com.sojpt.model.LkyTeacher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author Sohnny
 *
 */
@RestController
@RequestMapping("/admin/teacher/")
//@Before({AdminLoginInterceptor.class, AdminPopedomInterceptor.class})
public class LkyTeacherAdminController extends IViewController {
    @GetMapping("list")
    public void list() {
        LkyTeacher lkyTeacher = getModel(LkyTeacher.class, "lkyTeacher");
        SqlPara sqlPara = Db.getSqlPara("lky_teacher.select", getKv(lkyTeacher));
        Page<Record> page = Db.paginate(pageNumber(), pageSize(), sqlPara);
        renderJson(page);
    }

    @Before(LkyTeacherAddValidator.class)
    @PostMapping("save")
    public void save() {
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/teacher/save");
        LkyTeacher lkyTeacher = getModel(LkyTeacher.class, "lkyTeacher");
        lkyTeacher.setId(UUID.randomUUID().toString().replace("-", ""));
        lkyTeacher.setCreationTime(new Date());
        lkyTeacher.setUpdateTime(new Date());
        if(lkyTeacher.save()) {
            renderJson(Ret.ok());
            dicPopedom.setPopedomName("添加成功");
        } else {
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("添加失败");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }

    @Before(LkyTeacherUpdateValidator.class)
    @PostMapping("update")
    public void update() {
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/teacher/save");
        LkyTeacher lkyTeacher = getModel(LkyTeacher.class, "lkyTeacher");
        lkyTeacher.setUpdateTime(new Date());
        if(lkyTeacher.update()) {
            renderJson(Ret.ok());
            dicPopedom.setPopedomName("更新成功");
        } else {
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("更新失败");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }

    @PostMapping("delete")
    public void delete() {
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/teacher/delete");
        LkyTeacher lkyTeacher = getModel(LkyTeacher.class, "lkyTeacher");
        String tid=lkyTeacher.getId();
        System.out.println("tid=" + tid);
        Integer count = Db.queryInt("select count(lct.id) from lky_class_teacher lct where lct.tid=?", tid);
        if (count == 0){
            if(LkyTeacher.dao.deleteById(lkyTeacher.getId())) {
                renderJson(Ret.ok());
                dicPopedom.setPopedomName("删除成功");
            } else {
                renderJson(Ret.fail());
                dicPopedom.setPopedomName("删除失败");
            }
        }else {
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("老师已分配班级！");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }

    @PostMapping("batchDelete")
    public void batchDelete() {
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/teacher/batchDelete");
        String[] ids = getPara("ids").split(",");
        SqlPara sqlPara = Db.getSqlPara("lky_teacher.deleteByIds", Kv.by("idList", Arrays.asList(ids)));
        if(Db.update(sqlPara) > 0) {
            renderJson(Ret.ok());
            dicPopedom.setPopedomName("批量删除成功");
        } else {
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("批量删除失败");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }
}